<template>
  <div>
    <div class="back"></div>
    <div class="banner">
      <div class="imgs">
        <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
          <van-swipe-item v-for="(item, index) in banners" :key="index">
            <img :src="item.pic" alt="" />
          </van-swipe-item>
        </van-swipe>
      </div>
    </div>

    <div class="remLists">
      <div class="title">推荐</div>
      <div class="content">
        <ul>
          <li
            v-for="(item, index) in song_sheet"
            :key="index"
            @click="goMusicllist(item.id, item.playCount)"
          >
            <img :src="item.picUrl" alt="" />
            <span class="remd_lnum"
              ><span class="iconfont icon-erji"></span>
              {{ item.playCount | playcount }}</span
            >
            <p>{{ item.name }}</p>
          </li>
        </ul>
      </div>
    </div>
    <br />
    <div class="newson">
      <h3 class="tit">最新音乐</h3>
      <ul>
        <li
          v-for="(item, index) in newsong"
          :key="index"
          @click="gotoplay(item.id, item.name, item.picUrl, item.song.artists)"
          @click.once="pushlist"
        >
          <div class="imgs">
            <img :src="item.picUrl" />
          </div>
          <div>{{ item.name }}</div>
          <em>{{ item.song.artists[0].name | authorFn(item.song.artists) }}</em>
          <span class="iconfont icon-bofang"></span>
        </li>
      </ul>
    </div>
    <br />

    <br />
    <van-overlay :show="show" >
      <div class="wrapper" @click.stop>
        <van-loading type="spinner" />
      </div>
    </van-overlay>
  </div>
</template>

<script>
import { getBanner, song_sheet, getnewsong } from "../api/recommen";
export default {
  data() {
    return {
      banners: "",
      song_sheet: "",
      newsong: "",
      show: true,
    };
  },
  methods: {
    goMusicllist(id, count) {
      this.$router.push({
        path: "/recommend/musiclist",
        query: { id, count },
      });
    },
    gotoplay(...data) {
      let playData = {
        id: data[0],
        name: data[1],
        pic: data[2],
        author: data[3],
      };
      this.$store.commit("curListFn", playData);
      this.$store.commit("playflagFn", true);
    },
    pushlist() {
      let list = [];
      this.newsong.forEach((item) => {
        list.push({
          id: item.id,
          name: item.name,
          pic: item.picUrl,
          author: item.song.artists,
        });
      });
      this.$store.commit("allListFn", list);
    },
  },
  created() {
    getBanner().then((data) => {
      // 获取轮播图数据
      if (data.code == 200) {
        this.banners = data.banners;
      }
      //   console.log(data);
    });
    song_sheet().then((data) => {
      // 获取推荐歌单数据
      if (data.code == 200) {
        this.show = false;
        this.song_sheet = data.result;
      }
      // console.log(data.result[0].id);
    });
    getnewsong().then((data) => {
      if (data.code == 200) {
        this.newsong = data.result;
        // console.log(data.result);
        // console.log(data.result[0].song.album.artists[0].id); // 歌手id
      }
    });
  },
  filters: {
    playcount(value) {
      let w = value > 10000 ? value / 10000 : value; // 万计算
      let y = w > 10000 ? (w / 10000).toFixed(2) + "亿" : w.toFixed(1) + "万"; //亿计算
      return y;
    },
    authorFn(old, newd) {
      let str = "";
      newd.forEach((item) => {
        str = str + item.name + " / ";
      });
      str = str.substr(0, str.length - 2);
      return str;
    },
  },
};
</script>

<style lang="less">
.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}
.back{
  width: 100%;
  max-width: 640px;
  height: 120px;
  position:absolute;
  background-color: skyblue;
}
.banner {
  width: 100%;
  .imgs {
    margin: auto;
    width: 95%;
    border-radius: 10px;
    img {
      width: 100%;
      height: auto;
      border-radius: 10px;
    }
    .my-swipe {
      border-radius: 10px;
    }
  }
}
.remLists {
  .title {
    font-size: 0.34375rem;
    height: 1.125rem;
    line-height: 1.125rem;
    font-weight: bold;
    text-indent: 0.5em;
  }
  .content {
    ul {
      display: flex;
      flex-wrap: wrap;
      li {
        width: 33.33%;
        position: relative;
        margin-bottom: 16px;
        img {
          display: block;
          width: 96%;
          margin: 0 auto;
          border-radius: 3px;
        }
        p {
          font-size: 0.28125rem;
          color: #2f3035;
          height: 0.78125rem;
          overflow: hidden;
          line-height: 0.4375rem;
          padding: 0.15625rem 0.109375rem;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }
        .remd_lnum {
          position: absolute;
          top: 0px;
          right: 10px;
          font-size: 0.234375rem;
          color: #fff;
        }
      }
    }
  }
}
.loading {
  text-align: center;
}
.newson {
  .tit {
    font-size: 5vw;
    position: relative;
    padding-left: 0.125rem;
    color: #333;
    font-weight: 400;
    &::before {
      content: "";
      height: 0.5rem;
      width: 0;
      display: inline-block;
      border: 1px solid red;
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
    }
  }
  li {
    height: 1.875rem;
    border-bottom: 1px solid rgba(204, 204, 204, 0.748);
    position: relative;
    font-size: 5vw;
    .imgs {
      float: left;
      display: flex;
      height: 100%;
      width: 15vw;
      justify-content: center;
      align-items: center;
      img {
        width: 12vw;
        height: 12vw;
      }
    }
    div {
      float: left;
      line-height: 1.875rem;
      text-indent: 0.5em;
    }
    em {
      font-style: normal;
      color: rgb(151, 151, 151);
      position: absolute;
      left: 16vw;
      bottom: 10%;
      font-size: 3vw;
      text-indent: 0.5em;
    }
    span {
      position: absolute;
      right: 10px;
      font-size: 1.5em;
      top: 40%;
      color: rgb(151, 151, 151);
    }
  }
}
</style>